package com.flycent.hqhs.ui;

import java.util.UUID;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.widget.TextView;

import com.flycent.hqhs.App;
import com.flycent.hqhs.ExitManager;
import com.flycent.hqhs.ui.view.AbsCustomAlertDialog;
import com.flycent.hqhs.ui.view.LoadingDialog;
import com.flycent.hqhs.util.StringUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * 基础Activity
 * @author fbl
 */
public class BaseActivity extends FragmentActivity{

	public final String TAG = getClass().getSimpleName();

	private LoadingDialog mLoadingDialog;

	//---------------------------------------------------------

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		MobclickAgent.setDebugMode(true);
//      SDK在统计Fragment时，需要关闭Activity自带的页面统计，
//		然后在每个页面中重新集成页面统计的代码(包括调用了 onResume 和 onPause 的Activity)。
		MobclickAgent.openActivityDurationTrack(false);
//		MobclickAgent.setAutoLocation(true);
//		MobclickAgent.setSessionContinueMillis(1000);
		MobclickAgent.updateOnlineConfig(this);
		
		ExitManager.getInstance().addActivity(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		 MobclickAgent.onPageEnd(getClass().getName()); // 保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息 
		  MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		 MobclickAgent.onPageStart(getClass().getName());
		 MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		dismissLoadingDialog();
	}
	
	//---------------------------------------------------------

	/**
	 * 从 Intent 中取出存储的值。{@link #restoreExtras(Bundle)}
	 * @param intent
	 * @return
	 */
	protected boolean restoreExtras(final Intent intent) {
		if( intent == null ) {
			return false;
		}
		return restoreExtras(intent.getExtras());
	}

	/**
	 * 取得 bundle 中存储的值.
	 * <p>NOTE : 若要使用该方法，应该在子类中覆写此方法
	 * @param extra 
	 * @return true 成功取值  | false 取值不成功
	 */
	protected boolean restoreExtras(final Bundle extra) {
		return false;
	}

	/**
	 * bundle 是否为空
	 * @param extra
	 * @return true 为空 | false 不成功
	 */
	protected boolean isBundleEmpty(Bundle extra) {
		return extra == null || extra.isEmpty();
	}

	/**
	 * 显示正在加载的进度框
	 */
	public void showLoadingDialog(String msg) {
		dismissAllDialog();
		mLoadingDialog = new LoadingDialog(this);
		mLoadingDialog.showDialog(msg);
	}
	/**
	 * 显示正在加载的进度框
	 */
	public void showLoadingDialog(String msg,boolean isCancelable) {
		dismissAllDialog();
		mLoadingDialog = new LoadingDialog(this,isCancelable);
		mLoadingDialog.showDialog(msg);
	}

	/**
	 * 卸载加载进度框
	 */
	public void dismissLoadingDialog() {
		AbsCustomAlertDialog.dismissDialog(mLoadingDialog);
	}

	/**
	 * 卸载错误对话框
	 */
	protected void dismissMessageDialog() {
		//		AbsCustomAlertDialog.dismissDialog(mMessageDialog);
	}

	/**
	 * 卸载所有对话框
	 */
	protected void dismissAllDialog() {
		dismissLoadingDialog();
		dismissMessageDialog();
	}


	/**
	 * 验证 TextView 中的内容是否为空，若为空就弹出 Toast，内容为  errorStringRes
	 * @param textView
	 * @param errorStringRes
	 * @return true 验证未通过，false 验证通过
	 */
	protected boolean invalidateText(TextView textView, int errorStringRes) {
		return StringUtil.invalidateContent(getText(textView), errorStringRes);
	}

	/**
	 * 验证 TextView 中的内容是否为空
	 * @param textView
	 * @param errorStringRes
	 * @return true 验证未通过，false 验证通过
	 */
	protected boolean invalidateText(TextView textView) {
		return TextUtils.isEmpty(getText(textView));
	}

	/**
	 * get the text info...
	 * @param textView
	 * @return
	 */
	protected String getText( TextView textView ) {
		return textView.getText().toString();
	}

	/**
	 * 获取手机的唯一标识符  用于获取验证码
	 * @return String  mUniqueId
	 */
	@SuppressWarnings("unused")
	protected String getMyUUID(){

		final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);   
		final String tmDevice, tmSerial, tmPhone, androidId;   
		tmDevice = "" + tm.getDeviceId();  
		tmSerial = "" + tm.getSimSerialNumber();   
		androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);   
		UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());   
		//		mUniqueId = deviceUuid.toString();
		//		App.showLog("uuid="+mUniqueId);
		return deviceUuid.toString();
	}

	// 用于webhelper
	public static String getUUID(Context context){

		final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);   
		@SuppressWarnings("unused")
		final String tmDevice, tmSerial, tmPhone, androidId;   
		tmDevice = "" + tm.getDeviceId();  
		tmSerial = "" + tm.getSimSerialNumber();   
		androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);   
		UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());   
		//		mUniqueId = deviceUuid.toString();
		//		App.showLog("uuid="+mUniqueId);
		App.showLog("UUID==="+deviceUuid.toString());
		return deviceUuid.toString();
	}
}
